//
//  CommondHandle.m
//  RentTable
//
//  Created by mac on 2024/11/29.
//

#import "CommondHandle.h"

@implementation CommondHandle

+ (CAGradientLayer *)gradientTheView:(UIView *)gView bounds:(CGRect)bounds colors:(NSArray<UIColor *> *)colors startPoint:(CGPoint)startPoint endPoint:(CGPoint)endPoint hasCornerRadius:(CGFloat)cornerRadius shouldTakeBack:(BOOL)should
{
    CAGradientLayer *gradientLayer = [[CAGradientLayer alloc] init];
    if (cornerRadius > 0) {
        gradientLayer.cornerRadius = cornerRadius;
    }
    gradientLayer.frame = bounds;
    NSMutableArray *cgColors = [NSMutableArray array];
    for (UIColor *color in colors) {
        [cgColors addObject:(id)(color.CGColor)];
    }
    gradientLayer.colors = cgColors;
    gradientLayer.locations = @[@0, @1];
    [gradientLayer setStartPoint:startPoint];
    [gradientLayer setEndPoint:endPoint];

    if ([gView isKindOfClass:[UIButton class]]) {
        UIButton *button = (UIButton *)gView;
        UIGraphicsBeginImageContextWithOptions(bounds.size, NO, 0.0);
        [gradientLayer renderInContext:UIGraphicsGetCurrentContext()];
        UIImage *backgroundImage = UIGraphicsGetImageFromCurrentImageContext();
        UIGraphicsEndImageContext();

        [button setBackgroundImage:backgroundImage forState:UIControlStateNormal];
        return gradientLayer;
    }
    else {
        if (should) {
            if (gView.layer.sublayers.count > 1) {
                CALayer *subLayer = gView.layer.sublayers[0];
                [gView.layer replaceSublayer:subLayer with:gradientLayer];
            } else {
                [gView.layer insertSublayer:gradientLayer atIndex:0];
            }
        } else {
            [gView.layer addSublayer:gradientLayer];
        }
    }
    return gradientLayer;
}

/// - 根据字符串计算label高度
+ (CGFloat)getHeightLineWithString:(NSString *)string withWidth:(CGFloat)width withFont:(UIFont *)font minLineHeight:(CGFloat)minLineHeight {
    
    //1.1最大允许绘制的文本范围
    CGSize size = CGSizeMake(width, MAXFLOAT);
    //1.2配置计算时的行截取方法,和contentLabel对应
    NSMutableParagraphStyle *style = [[NSMutableParagraphStyle alloc] init];
    style.lineBreakMode = NSLineBreakByWordWrapping;
    //    [style setLineSpacing:8];
    style.minimumLineHeight = minLineHeight;
    //1.3配置计算时的字体的大小
    //1.4配置属性字典
    NSDictionary *dic = @{NSFontAttributeName:font, NSParagraphStyleAttributeName:style};
    
    //2.计算
    //如果想保留多个枚举值,则枚举值中间加按位或|即可,并不是所有的枚举类型都可以按位或,只有枚举值的赋值中有左移运算符时才可以
    CGSize resultSize = [string boundingRectWithSize:size options:NSStringDrawingUsesLineFragmentOrigin | NSStringDrawingUsesFontLeading  attributes:dic context:nil].size;
    CGFloat height =  ceilf(resultSize.height);
    
    return height;
    
}

/// 部分圆角
+ (void)cornerTheView:(UIView *)theView byCorner:(UIRectCorner)corner withRadias:(CGFloat)radias{
    if (theView.bounds.size.height < 2*radias) {
        CGRect rect = theView.frame;
        rect.size.height = 2*radias;
        theView.frame = rect;
    }
    UIBezierPath * path = [UIBezierPath bezierPathWithRoundedRect:theView.bounds byRoundingCorners:corner cornerRadii:CGSizeMake(radias, radias)];
    CAShapeLayer *maskLayer = [[CAShapeLayer alloc] init];
    maskLayer.frame = theView.bounds;
    maskLayer.path = path.CGPath;
    theView.layer.mask = maskLayer;

}

+ (BOOL)getLanguageRTL{
    if ([[[NSBundle mainBundle].preferredLocalizations firstObject] hasPrefix:@"ar"] || [[[NSBundle mainBundle].preferredLocalizations firstObject] hasPrefix:@"ur"] || [[[NSBundle mainBundle].preferredLocalizations firstObject] hasPrefix:@"fa"]) {
        return YES;
    }
    return NO;
}

/// 普通字符串转自定义行高字符串富文本
+ (NSAttributedString *)changeStrToCustomLineHeightAttributedStr:(NSString *)str lineHeight:(NSInteger)lineHegiht {
    NSMutableParagraphStyle *style = [[NSMutableParagraphStyle alloc] init];
    style.minimumLineHeight = lineHegiht;
    
    NSDictionary *dic = @{ NSParagraphStyleAttributeName:style};
    if (str.length == 0) {
        NSAttributedString *attr = [[NSAttributedString alloc] initWithString:@"" attributes:dic];
        return attr;
    }
    NSAttributedString *attr = [[NSAttributedString alloc] initWithString:str attributes:dic];
    return attr;
}


@end
